package com.swss.g.user.action;

import com.swss.g.base.BaseAction;
import com.swss.g.dept.service.DeptService;
import com.swss.g.user.bean.User;
import com.swss.g.user.service.UserService;
import com.swss.util.MD5;
import com.swss.util.ReadConfig;

public class LoginAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3016348148306946228L;
	/**
	 * bean
	 */

	private User user;
	private String userName;
	private String pwd;

	/**
	 * service
	 * 
	 * @return
	 */

	private UserService userService;
	private DeptService deptService;

	public String login() {
		try {

			if (userName != null && pwd != null) {
				MD5 md5 = new MD5();
				User u = new User();
				u.setUserName(userName);
				u.setPassword(md5.getMD5Str(pwd));

				user = this.userService.login(u);

			} else {
				return "loginError";
			}
			if (user == null) {
				this.msg = "用户名或密码不正确";
				return "loginError";
			} else {
				/**
				 * 获取配置文件中的Ip地址，报表打印输出使用 start 获取00级单位名称
				 */
				ReadConfig readconfig = new ReadConfig();

				// System.out.println("**"+readconfig.readPrintIPPath("printIpPath")+"=="+readconfig.readPrintIPPath("zeroDeptName"));
				this.setSessionAttr("user", user);

				this.setSessionAttr("printIp", readconfig
						.readPrintIPPath("printIpPath"));
				this.setSessionAttr("zeroDept", deptService
						.findOneDept("00"));

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String logout() {
		// clear All session
		this.setSessionAttr("user", null);
		this.setSessionAttr("searchBean", null);
		this.getSessionAttr("printIp", null);
		this.getSessionAttr("zeroDept", null);
		return SUCCESS;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public DeptService getDeptService() {
		return deptService;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

}
